//
//  XMMeController.m
//  XMWeibo
//
//  Created by chi on 14-8-20.
//  Copyright (c) 2014年 xiaoming. All rights reserved.
//

#import "XMMeController.h"

#import "XMSystemSettingController.h"
#import "XMDiscoverController.h"

#import "XMUserInfoView.h"
#import "XMLoadUserInfoTool.h"
#import "XMWeiboAccount.h"

#define kHeaderH (115.0)

@interface XMMeController ()

// 头部
@property (nonatomic, strong) UIView *headerView;


// 用户视图
@property (nonatomic, weak) XMUserInfoView *userView;


// 头部视图高度
@property (nonatomic, assign) CGFloat headerHeight;

@end

@implementation XMMeController



- (void)viewDidLoad
{
    [super viewDidLoad];
    
   
    if (isIOS7) {
         self.headerHeight = 160.0;
    }
    else {
        self.headerHeight = 135.0;
    }
    
    // 添加头部视图
    [self setupHeaderView];
    
    
    // 设置用户信息
    XMWeiboUser *weiboUser = [XMWeiboUser loadWeiboUserFromFile];
    if (weiboUser) {
        self.userView.user = weiboUser;
    }
    
    
    //标题栏右边按钮
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"设置" style:UIBarButtonItemStylePlain target:self action:@selector(clickSetting)];
     
    // 添加分组
    [self setGroup1];
    [self setGroup2];
    [self setGroup3];
    [self setGroup4];
    
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    // 网络加载用户信息
    [self loadUserInfo];
}

/**
 *  从新浪加载微博帐号信息
 */
- (void)loadUserInfo
{
    
    XMHttpLoadUserInfoParams *params = [XMHttpLoadUserInfoParams params];
    params.uid = @([XMWeiboAccount account].uid);
    
    
    [XMLoadUserInfoTool httpLoadUserInfoWithParams:params success:^(XMHttpLoadUserInfoResult *result) {
        XMWeiboUser *user = result;
        
        self.userView.user = user;
        
        //归档
        [user saveWeiboUserToFile];
        
    } failure:^(NSError *error) {
        
        XMLog(@"%@请求失败，失败原因: %@", k_statuses_home_timeline, error);
    }];
    
}

- (void)setupHeaderView
{
    self.headerView = [[UIView alloc]init];
    
    self.headerView.frame = CGRectMake(0.0, 0.0, 0.0, self.headerHeight);
    XMUserInfoView *userView = [[XMUserInfoView alloc]init];
    [self.headerView addSubview:userView];
    self.userView = userView;
    
    CGFloat width = self.view.bounds.size.width;
    
    CGFloat userViewY = 35.0;
    CGFloat userViewX = 5.0;
    if (!isIOS7) {
        userViewY = 15.0;
        userViewX = 10.0;
    }
    
    
    userView.frame = CGRectMake(userViewX, userViewY, width - userViewX * 2, kHeaderH);
 
}


#pragma mark - tableview 代理
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return self.headerView;
    }
   
    return [super tableView:tableView viewForHeaderInSection:section];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return self.headerHeight;
    }
   
    return [super tableView:tableView heightForHeaderInSection:section];
}

#pragma mark - 添加分组

- (void)setGroup1
{
    XMGroupInfo *group1 = [self addGroup];
    
    XMArrowSettingItem *newFriend = [XMArrowSettingItem arrowCellItemWithTitle:@"新的好友" subTitle:nil icon:@"new_friend" class:nil];
    newFriend.badgeValue = @"25";
    
    
    group1.cellItems = @[newFriend];
}

- (void)setGroup2
{
    XMGroupInfo *group2 = [self addGroup];
    
    XMArrowSettingItem *myPhoto = [XMArrowSettingItem arrowCellItemWithTitle:@"我的相册" subTitle:@"(109)" icon:@"album" class:nil];
    
    XMArrowSettingItem *myCollect = [XMArrowSettingItem arrowCellItemWithTitle:@"我的收藏" subTitle:@"(0)" icon:@"collect" class:nil];
    myCollect.badgeValue = @"99+";
    
     XMArrowSettingItem *myAttitude = [XMArrowSettingItem arrowCellItemWithTitle:@"赞" subTitle:@"(35)" icon:@"like" class:nil];
    
    
    group2.cellItems = @[myPhoto, myCollect, myAttitude];
}

- (void)setGroup3
{
    XMGroupInfo *group3 = [self addGroup];
    
    XMArrowSettingItem *pay = [XMArrowSettingItem arrowCellItemWithTitle:@"微博支付" subTitle:nil icon:@"pay" class:nil];
    pay.badgeValue = @"new";
    
    XMArrowSettingItem *vip = [XMArrowSettingItem arrowCellItemWithTitle:@"个性化" subTitle:nil icon:@"vip" class:nil];

    
    group3.cellItems = @[pay, vip];

}

- (void)setGroup4
{
    XMGroupInfo *group4 = [self addGroup];
    
    XMArrowSettingItem *card = [XMArrowSettingItem arrowCellItemWithTitle:@"我的名片" subTitle:nil icon:@"card" class:nil];
    card.badgeValue = @"哈哈";
    
    XMArrowSettingItem *draft = [XMArrowSettingItem arrowCellItemWithTitle:@"草稿箱" subTitle:nil icon:@"draft" class:nil];
    
    
    group4.cellItems = @[card, draft];
    
}


#pragma mark -
- (void)clickSetting
{
    XMSystemSettingController *controller = [[XMSystemSettingController alloc]init];
    
    [self.navigationController pushViewController:controller animated:YES];
}

@end
